﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Pavilion.DesignerTool
{
    public class OriginTool : IDrawTool
    {
        public string Name { get { return this.GetType().Name; } }
        public float Size { get; set; }
        public int Opacity { get; set; }

        private Vector2 distanceFromOriginPosition;
        private bool isMoving;

        public void BeginExecution(DrawingBoardData data)
        {
            Vector2 origin = GetOriginPosition(data);
            distanceFromOriginPosition = (data.FrameData.Origin * data.Scale) - data.PenPosition;
            isMoving = PolygonBox.IsInsideRectangle(new Rectangle((int)origin.X - 10, (int)origin.Y - 10, 20, 20), data.PenPosition);
        }

        public void Execute(DrawingBoardData data)
        {
            if (isMoving)
                data.FrameData.Origin = (data.PenPosition + distanceFromOriginPosition) / data.Scale;
        }

        public void EndExecution(DrawingBoardData data, Action creatUndoPointAction)
        {
            Execute(data);

            isMoving = false;
        }

        public void DrawInterface(DrawingBoardData data)
        {
            if (data.FrameData == null)
                return;

            DrawManager.Instance.DrawCircle(GetOriginPosition(data), 5f, Color.Red);
            DrawManager.Instance.DrawCircle(GetOriginPosition(data), 4f, Color.White);
        }

        private Vector2 GetOriginPosition(DrawingBoardData data) { return data.CurrentTexturePositionOnBoard + (data.FrameData.Origin * data.Scale); }
    }
}
